#include "GY_MultilevelTree.h"

#include <QHeaderView>
#include <QPainter>

GY_MultilevelTree::GY_MultilevelTree(QWidget *parent, QVariant cuttentMultilevelID)
    :GY_TreeView(parent),m_CuttentMultilevelID(cuttentMultilevelID)
{

}
/**
 * @brief addMultileveTree 添加多级树
 * @param multileveID 多级ID
 * @param parentRowID 父级行ID
 * @return
 */
bool GY_MultilevelTree::addMultileveTree(const QVariant &multileveID, const QVariant &parentRowID)
{
    //判定是否已经有该层级
    if(m_MultilevelInfoMap.contains(multileveID))
    {
        return false;
    }
    if(m_TreeNodeInfo.contains(parentRowID))
    {
        Tree_Node_Info* pNodeInfo=m_TreeNodeInfo.value(parentRowID);
        //判定行是否已经包含了层级
        if(pNodeInfo->pNodeItem->data(TreeDataType::ContainMultileveID).isValid())
        {
            return false;
        }
        MultilevelInfo info;
        info.parentRowID=parentRowID;
        info.multilevelID=multileveID;
        info.parentMultilevelID=m_CuttentMultilevelID;
        info.pMultilevelTree=new GY_MultilevelTree(nullptr,multileveID);
        m_MultilevelInfoMap.insert(multileveID,info);
        QStandardItem* pNodeItem=new QStandardItem;
        pNodeInfo->pNodeItem->appendRow(QList<QStandardItem*>()<<pNodeItem);
        pNodeInfo->pNodeItem->setData(multileveID,TreeDataType::ContainMultileveID);
        this->setIndexWidget(pNodeItem->index(),info.pMultilevelTree);
        m_SpanInfoMap.insert(pNodeItem,SpanInfo()={header()->logicalIndex(0),header()->logicalIndex(header()->count()-1)});
        return true;
    }
    foreach(MultilevelInfo info,m_MultilevelInfoMap)
    {
        if(info.pMultilevelTree->addMultileveTree(multileveID,parentRowID))
        {
            return true;
        }
    }
    return false;
}
/**
 * @brief deleteMultileveTree 删除多级树
 * @param multileveID 多级ID
 */
void GY_MultilevelTree::deleteMultileveTree(const QVariant &multileveID)
{
    //判定是否已经有该层级
    if(m_MultilevelInfoMap.contains(multileveID))
    {
        delete m_MultilevelInfoMap.value(multileveID).pMultilevelTree;
        m_TreeNodeInfo.value(m_MultilevelInfoMap.value(multileveID).parentRowID)->pNodeItem->setData(QVariant(),TreeDataType::ContainMultileveID);
        m_MultilevelInfoMap.remove(multileveID);
        return;
    }
    foreach(MultilevelInfo info,m_MultilevelInfoMap)
    {
        info.pMultilevelTree->deleteMultileveTree(multileveID);
    }
}
/**
 * @brief addColumn 添加列数
 * @param columnName 添加的列名
 * @param multileveID 多级ID
 */
void GY_MultilevelTree::addMultileveTreeColumn(const QString &columnName, const QVariant &multileveID)
{
    //判定是否已经有该层级
    if(m_MultilevelInfoMap.contains(multileveID))
    {
        m_MultilevelInfoMap.value(multileveID).pMultilevelTree->addColumn(columnName);
        return;
    }
    foreach(MultilevelInfo info,m_MultilevelInfoMap)
    {
        info.pMultilevelTree->addMultileveTreeColumn(columnName,multileveID);
    }
}
/**
 * @brief insterColumn 插入列
 * @param index 插入在第几列
 * @param columnName 插入的列名
 * @param multileveID 多级ID
 * @return 返回插入列是否成功
 */
bool GY_MultilevelTree::insterMultileveTreeColumn(const int index, const QString &columnName, const QVariant &multileveID)
{
    //判定是否已经有该层级
    if(m_MultilevelInfoMap.contains(multileveID))
    {
       return m_MultilevelInfoMap.value(multileveID).pMultilevelTree->insterMultileveTreeColumn(index,columnName,multileveID);
    }
    foreach(MultilevelInfo info,m_MultilevelInfoMap)
    {
        if(info.pMultilevelTree->insterMultileveTreeColumn(index,columnName,multileveID))
        {
            return true;
        }
    }
    return false;
}
/**
 * @brief deleteColumn 删除列
 * @param index 删除的第几列
 * @param multileveID 多级ID
 * @return 返回删除列是否成功
 */
bool GY_MultilevelTree::deleteMultileveTreeColumn(const int index, const QVariant &multileveID)
{
    //判定是否已经有该层级
    if(m_MultilevelInfoMap.contains(multileveID))
    {
        m_MultilevelInfoMap.value(multileveID).pMultilevelTree->deleteMultileveTreeColumn(index,multileveID);
        return true;
    }
    foreach(MultilevelInfo info,m_MultilevelInfoMap)
    {
        if(info.pMultilevelTree->deleteMultileveTreeColumn(index,multileveID))
        {
            return true;
        }
    }
    return false;
}
/**
 * @brief isHaveRow  是否有行
 * @param rowID  行ID
 * @return 是否存在该行
 */
bool GY_MultilevelTree::isHaveRow(const QVariant &rowID)
{
    if(m_TreeNodeInfo.contains(rowID))
    {
        return true;
    }
    foreach(MultilevelInfo info,m_MultilevelInfoMap)
    {
        if(info.pMultilevelTree->isHaveRow(rowID))
        {
            return true;
        }
    }
    return false;
}
/**
 * @brief addRowData  添加行数据
 * @param rowId 行Id
 * @param rowData 行数据
 * @param nodeParentId 父级Id
 * @param multileveID 多级ID
 * @return 返回添加是否成功
 */
bool GY_MultilevelTree::addMultileveTreeRowData(const QVariant &rowId, const QVector<QVariant> &rowData, const QVariant &nodeParentId,const QVariant& multileveID, const QVariant &iconType)
{
    //判定是否已经有该层级
    if(m_TreeNodeInfo.contains(nodeParentId))
    {
        this->addRowData(rowId,rowData,nodeParentId,iconType);
        return true;
    }
    else if(!nodeParentId.isValid()&&m_MultilevelInfoMap.contains(multileveID))
    {
        return  m_MultilevelInfoMap.value(multileveID).pMultilevelTree->addRowData(rowId,rowData,nodeParentId,iconType);
    }
    foreach(MultilevelInfo info,m_MultilevelInfoMap)
    {
        if(info.pMultilevelTree->addMultileveTreeRowData(rowId,rowData,nodeParentId,iconType))
        {
            return true;
        }
    }
    return false;
}
/**
 * @brief insterRowData   插入行数据
 * @param rowId 节点id
 * @param rowData 插入的行数据
 * @param index 引索第几个
 * @param nodeParentId 父级Id
 * @param multileveID 多级ID
 * @return 返回插入行数据是否成功
 */
bool GY_MultilevelTree::insterMultileveTreeRowData(const QVariant &rowId, const QVector<QVariant> &rowData, const int index, const QVariant &nodeParentId, const QVariant &multileveID, const QVariant iconType)
{
    //判定是否已经有该层级
    if(m_TreeNodeInfo.contains(nodeParentId))
    {
        return this->insterRowData(rowId,rowData,index,nodeParentId,iconType);
    }
    else if(!nodeParentId.isValid()&&m_MultilevelInfoMap.contains(multileveID))
    {
        return  m_MultilevelInfoMap.value(multileveID).pMultilevelTree->insterRowData(rowId,rowData,index,nodeParentId,iconType);
    }
    foreach(MultilevelInfo info,m_MultilevelInfoMap)
    {
        if(info.pMultilevelTree->insterMultileveTreeRowData(rowId,rowData,index,nodeParentId,iconType))
        {
            return true;
        }
    }
    return false;
}
/**
 * @brief deleteRow  删除指定行
 * @param rowId 删除行的id号
 * @return 返回删除是否成功
 */
bool GY_MultilevelTree::deleteMultileveTreeRow(const QVariant &rowId)
{
    //判定是否已经有该层级
    if(m_TreeNodeInfo.contains(rowId))
    {
       return this->deleteRow(rowId);
    }
    foreach(MultilevelInfo info,m_MultilevelInfoMap)
    {
        if(info.pMultilevelTree->deleteMultileveTreeRow(rowId))
        {
            return true;
        }
    }
    return false;
}
/**
 * @brief setRowData  修改行数据
 * @param rowId  修改行id
 * @param rowData  要修改的数据
 * @return 是否修改成功
 */
bool GY_MultilevelTree::setMultileveTreeRowData(const QVariant &rowId, const QVector<QVariant> &rowData)
{
    //判定是否已经有该层级
    if(m_TreeNodeInfo.contains(rowId))
    {
        return this->setRowData(rowId,rowData);
    }
    foreach(MultilevelInfo info,m_MultilevelInfoMap)
    {
        if(info.pMultilevelTree->setMultileveTreeRowData(rowId,rowData))
        {
            return true;
        }
    }
    return false;
}
/**
 * @brief setCellData  修改单元格数据
 * @param rowID  修改的行id
 * @param columnIndex  修改第几列
 * @param cellData  要修改的数据
 * @return 是否修改成功
 */
bool GY_MultilevelTree::setMultileveTreeCellData(const QVariant &rowId, const int columnIndex, const QVariant &cellData)
{
    //判定是否已经有该层级
    if(m_TreeNodeInfo.contains(rowId))
    {
       return  this->setCellData(rowId,columnIndex,cellData);
    }
    foreach(MultilevelInfo info,m_MultilevelInfoMap)
    {
        if(info.pMultilevelTree->setMultileveTreeCellData(rowId,columnIndex,cellData))
        {
            return true;
        }
    }
    return false;
}
/**
 * @brief getRowData  通过id获取行数据
 * @param rowID 要获取的节点id
 * @return 返回id对应的行数据
 */
QVector<QVariant> GY_MultilevelTree::getMultileveTreeRowData(const QVariant &rowId)
{
    //判定是否已经有该层级
    if(m_TreeNodeInfo.contains(rowId))
    {
       return this->getRowData(rowId);
    }
    QVector<QVariant> rowDatas;
    foreach(MultilevelInfo info,m_MultilevelInfoMap)
    {
        if(info.pMultilevelTree->isHaveRow(rowId))
        {
            return info.pMultilevelTree->getMultileveTreeRowData(rowId);
        }
    }
    return QVector<QVariant>();
}
/**
 * @brief getCellData  获取单元格数据
 * @param rowID  行id
 * @param columnIndex  索引第几列
 * @param data  单元格的数据
 * @return 返回是否获取成功
 */
bool GY_MultilevelTree::getMultileveTreeCellData(const QVariant &rowId, const int columnIndex, QVariant &data)
{
    //判定是否已经有该层级
    if(m_TreeNodeInfo.contains(rowId))
    {
        return this->getCellData(rowId,columnIndex,data);
    }
    foreach(MultilevelInfo info,m_MultilevelInfoMap)
    {
        if(info.pMultilevelTree->getMultileveTreeCellData(rowId,columnIndex,data))
        {
            return true;
        }
    }
    return false;
}
//绘制行
void GY_MultilevelTree::drawRow(QPainter *paniter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStandardItem* pItem=((QStandardItemModel*)(this->model()))->itemFromIndex(index);
    if(m_SpanInfoMap.contains(pItem))
    {
        QStyleOption opt=option;
        if(selectionModel()->isSelected(index))
        {
            opt.state!=QStyle::State_Selected;
        }
        int fistSection=m_SpanInfoMap.value(pItem).firstSection;
        int lastSection=m_SpanInfoMap.value(pItem).lastSection;
        int left=header()->sectionViewportPosition(fistSection);
        int right=header()->sectionViewportPosition(lastSection)+header()->sectionSize(lastSection);
        int indent=2*indentation();
        left+=indent;
        opt.rect.setX(left);
        opt.rect.setWidth(right-left);
        itemDelegate(index)->paint(paniter,option,index);
        QWidget* pWidget=this->indexWidget(index);
        pWidget->resize(right-left,pWidget->height());
    }
    else
    {
       GY_TreeView::drawRow(paniter,option,index);
    }
}
